// function hecheng(){
//     draw(function(){
//         document.getElementById('imgBox').innerHTML='< img src="'+base64[0]+'">';
//     })
// }

var base64 = [];

function draw(data, fn) {
  var ctx = wx.createCanvasContext('c'),
    // ctx=c.getContext('2d'),
    len = data.length;
  ctx.width = 30;
  ctx.height = (ctx.width - 10) + 125;
  // ctx.rect(0,0,ctx.width,ctx.height);
  ctx.fillRect(0, 0, ctx.width, ctx.height);
  ctx.fillStyle = '#fff';

  ctx.fill();

  function drawing(n) {
    if (n < len) {
      // var img=new Image;
      // //img.crossOrigin = 'Anonymous'; //解决跨域
      // img.src=data[n];
      // img.onload=function(){
      //     if(n==0){
      //         ctx.drawImage(img,5,5,c.width-10,(c.width-10));//距 left  top  width   height
      //     }else{
      //         ctx.drawImage(img,0,(c.width-10)+5,120,120);
      //     }
      //     drawing(n+1);//递归
      // }
      // wx.chooseImage({
      //   success: function(res){
      //     if(n==0){
      //         ctx.drawImage(res.tempFilePaths[0],5,5,ctx.width-10,(ctx.width-10));//距 left  top  width   height
      //     }else{
      //         ctx.drawImage(res.tempFilePaths[n],0,(ctx.width-10)+5,120,120);
      //     }
      //     ctx.draw()
      //     drawing(n+1);//递归
      //   }
      // })
      var that = this;
      //画布canvas
      const ctx = wx.createCanvasContext('myCanvas');

      ctx.drawImage('https://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83ep05ABFlMU5oUc1O5Km3aefZY1KblKNTibC8qdgyRyKvcUpkBmqmZD3DxDibCSyZehZ0DC2DLeZ1neg/0', 0, 0, 120, 100)
      ctx.draw();

      ctx.setFontSize(20);
      ctx.fillText('NaN', 120, 100);
      ctx.draw();
    } else {
      //保存生成作品图片
      wx.canvasToTempFilePath('c');
      // base64.push(ctx.toDataURL("image/jpeg",1));
      // fn();
    }
  }
  drawing(0);
  return base64;
}

function parseURL(url) {
  if (url && url.indexOf("?") == -1) return {}
  var startIndex = url.indexOf("?") + 1;
  var str = url.substr(startIndex);
  var strs = str.split("&");
  var param = {}
  for (var i = 0; i < strs.length; i++) {
    var result = strs[i].split("=");
    var key = result[0];
    var value = result[1];
    param[key] = value;
  }
  return param
}
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') 
  // + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
function getDateStr(today, addDayCount) {
  var dd;
  if (today) {
    dd = new Date(today);
  } else {
    dd = new Date();
  }
  dd.setDate(dd.getDate() + addDayCount);//获取AddDayCount天后的日期 
  var y = dd.getFullYear();
  var m = dd.getMonth() + 1;//获取当前月份的日期 
  var d = dd.getDate();
  if (m < 10) {
    m = '0' + m;
  };
  if (d < 10) {
    d = '0' + d;
  };
  return y + "-" + m + "-" + d;
}
module.exports = {
  draw: draw,
  parseURL,
  formatTime: formatTime,
  getDateStr: getDateStr
}